use std::{
    fs::File,
    io::{self, BufRead},
    path::Path,
};

fn main() {
    //f1();
    //f2();
    //f3();
    f4();
}

fn f4() {
    let s = r#""南昌": ["九江", "新余"],"#;
    let s_s: Vec<&str> = s.clone().split(":").collect();
    //dbg!(&s_s);
    let key_name = s_s[0];
    //dbg!(&key_name);
    let i = 1_usize;
    let to_str = format!(r#""{}""#, i);
    //dbg!(&to_str);
    let s = s.replace(key_name, &to_str);
    let to_str2 = format!("[{},", key_name);
    let s = s.replace("[", &to_str2);
    dbg!(s);
}

fn f3() {
    // （所有的类型标注都不是必需的）
    // 一个对只读内存中分配的字符串的引用
    let pangram: &'static str = "the quick brown fox jumps over the lazy dog";
    println!("Pangram: {}", pangram);

    // 逆序迭代单词，这里并没有分配新的字符串
    println!("Words in reverse");
    for word in pangram.split_whitespace().rev() {
        println!("> {}", word);
    }

    // 复制字符到一个 vector，排序并移除重复值
    let mut chars: Vec<char> = pangram.chars().collect();
    chars.sort();
    chars.dedup();

    // 创建一个空的且可增长的 `String`
    let mut string = String::new();
    for c in chars {
        // 在字符串的尾部插入一个字符
        string.push(c);
        // 在字符串尾部插入一个字符串
        string.push_str(", ");
    }
    println!("current string: {}", string);
    // 这个缩短的字符串是原字符串的一个切片，所以没有执行新的分配操作
    let chars_to_trim: &[char] = &[' ', ','];
    let trimmed_str: &str = string.trim_matches(chars_to_trim);
    println!("Used characters: {}", trimmed_str);

    // 堆分配一个字符串
    let alice = String::from("I like dogs");
    // 分配新内存并存储修改过的字符串
    let bob: String = alice.replace("dog", "cat");

    println!("Alice says: {}", alice);
    println!("Bob says: {}", bob);
}

fn f2() {
    let s = String::from("abcdef");
    println!("{}", s);
    //let mut r: Vec<char> = s.chars().rev().collect();
    // let mut t = String::new();
    // for c in r {
    //     t.push(c);
    // }
    // println!("{}", t);
    //println!("{}", s == t);
    let r: String = s.chars().rev().into_iter().collect();
    println!("{}", r);
    println!("{}", s.eq(&r));
}

fn f1() {
    // println!("Hello, world!");
    let mut i = 0;
    let mut days: Vec<u8> = Vec::new();
    if let Ok(lines) = read_line("data.txt") {
        for line in lines {
            if let Ok(l) = line {
                //println!("{:?}", l);
                days.extend(cal_days(l));
            }
            i += 1;
            // if i == 2 {
            //     break;
            // }
        }
    }
    println!("{:?}", days);
    println!("{:?}", days.len());
}

fn read_line<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
    P: AsRef<Path>,
{
    let f = File::open(filename)?;
    Ok(io::BufReader::new(f).lines())
}

fn cal_days(line: String) -> Vec<u8> {
    let tmp: Vec<&str> = line.split(" ").filter(|s| !s.is_empty()).collect();
    //dbg!(tmp);
    let n: Vec<u8> = tmp
        .into_iter()
        .map(|s| -> u8 {
            //dbg!(s);
            let t: Vec<&str> = s.split("-").collect();
            let mut r: u8 = 0u8;
            if t[1] != "1" {
                r += 31;
            }
            r += t[2].parse::<u8>().unwrap() - 1;
            r
        })
        .collect();

    //println!("{:?}", n);
    n
}

fn cal_days_2(line: &str) -> Vec<u8> {
    let x = line
        .split(" ")
        .filter_map(|s| {
            if !s.is_empty() {
                let t: Vec<&str> = s.split("-").collect();
                let mut r: u8 = 0u8;
                if t[1] != "1" {
                    r += 30;
                }
                r += t[2].parse::<u8>().unwrap();
                Some(r)
            } else {
                None
            }
        })
        .collect();

    x

    // let tmp: Vec<&str> = line.split(" ").filter(|s| !s.is_empty()).collect();
    // //dbg!(tmp);
    // let n: Vec<u8> = tmp
    //     .into_iter()
    //     .map(|s| -> u8 {
    //         //dbg!(s);
    //         let t: Vec<&str> = s.split("-").collect();
    //         let mut r: u8 = 0u8;
    //         if t[1] != "1" {
    //             r += 30;
    //         }
    //         r += t[2].parse::<u8>().unwrap();
    //         r
    //     })
    //     .collect();

    // //println!("{:?}", n);
    // n
}
